package com.ningjingjing.service;


import com.ningjingjing.dao.AlbumDao;
import com.ningjingjing.dao.StoryDao;
import com.ningjingjing.model.Album;

import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;


public class AlbumService {
    private final AlbumDao albumDao = new AlbumDao();
    private final StoryDao storyDao = new StoryDao();

    // 新建专辑
    public int save(Connection c, int uid, String name, InputStream coverIS, String cover, String filename) throws IOException, SQLException {
        try (OutputStream os = new FileOutputStream(filename)) {
            byte[] buf = new byte[1024];

            while (true) {
                int len = coverIS.read(buf);
                if (len == -1) {
                    break;
                }

                os.write(buf, 0, len);
            }

            os.flush();
        }

        return albumDao.insert(c, uid, name, cover);
    }

    // 获取专辑详情
    public Album get(Connection c, int aid) throws SQLException {
        Album album = albumDao.selectOneByAid(c, aid);
        album.storyList = storyDao.selectListByAid(c, aid);

        return album;
    }
}
